package top.chmin.project.cloud.common.dict.data.auto;

// field import auto
// #bind.field.dictOrder#

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.chmin.project.cloud.common.entity.Page;
import top.chmin.project.cloud.common.tool.DatetimeTool;
import top.chmin.project.cloud.common.tool.LocalTool;

import java.time.LocalDateTime;

/**
 * chmin
 * 2023-12-25 09:15:39
 * cloud-builder
 */
@Slf4j
@Component
public class DictDataPlugin {

    public void checkSave(DictDataSave.Param param) {
        log.debug("checkSave-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("保存对象不能为空");
        }
    }

    public void beforeSave(DictData dictData) {
        log.debug("beforeSave-DictData: {}", JSONUtil.toJsonStr(dictData));
        dictData.setCreateBy(LocalTool.getUserId());
        dictData.setUpdateBy(LocalTool.getUserId());
        dictData.setCreateTime(LocalDateTime.now());
        dictData.setUpdateTime(LocalDateTime.now());
    }

    public void afterSave(DictDataSave.Param param, DictData dictData, int result) {
        log.debug("afterSave-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSave-dictData: {}", JSONUtil.toJsonStr(dictData));
        log.debug("afterSave-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkUpdateById(DictDataUpdateById.Param param) {
        log.debug("checkUpdateById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("更新对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("更新对象id不能为空");
        }
    }

    public void beforeUpdateById(DictData dictData) {
        log.debug("beforeUpdateById-dictData: {}", JSONUtil.toJsonStr(dictData));
        dictData.setUpdateBy(LocalTool.getUserId());
        dictData.setUpdateTime(LocalDateTime.now());
    }

    public void afterUpdateById(DictDataUpdateById.Param param, DictData dictData, int result) {
        log.debug("afterUpdateById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterUpdateById-dictData: {}", JSONUtil.toJsonStr(dictData));
        log.debug("afterUpdateById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkDeleteById(DictDataDeleteById.Param param) {
        log.debug("checkDeleteById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("删除对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("删除对象id不能为空");
        }
    }

    public void afterDeleteById(DictDataDeleteById.Param param, int result) {
        log.debug("afterDeleteById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterDeleteById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectById(DictDataSelectById.Param param) {
        log.debug("checkSelectById-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("查询对象不能为空");
        }
        if (param.getId() == null) {
            throw new RuntimeException("查询对象id不能为空");
        }
    }

    public void beforeSelectById(DictDataSelectById.Param param, DictDataFPC fpc) {
        log.debug("beforeSelectById-param: {}", JSONUtil.toJsonStr(param));

    }

    public void afterSelectById(DictDataSelectById.Param param, DictDataSelectById.Result result) {
        log.debug("afterSelectById-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectById-result: {}", JSONUtil.toJsonStr(result));

    }

    public void checkSelectPage(DictDataSelectPage.Param param) {
        log.debug("checkSelectPage-param: {}", JSONUtil.toJsonStr(param));
        if (param == null) {
            throw new RuntimeException("分页对象不能为空");
        }
    }

    public void beforeSelectPage(DictDataSelectPage.Param param, DictDataFPC fpc) {
        log.debug("beforeSelectPage-param: {}", JSONUtil.toJsonStr(param));
// field beforeSelectPage auto
// #bind.field.dictCode#
        fpc.dictCode().eq(param.getDictCode());
// $bind.field.dictCode$

// #bind.field.dictOrder#
        fpc.dictOrder().eq(param.getDictOrder());
// $bind.field.dictOrder$

// #bind.field.createBy#
        fpc.createBy().eq(param.getCreateBy());
// $bind.field.createBy$

// #bind.field.updateBy#
        fpc.updateBy().eq(param.getUpdateBy());
// $bind.field.updateBy$

// #bind.field.createTime#
        fpc.createTime().ge(param.getCreateTime() == null || param.getCreateTime().length == 0 ? null : param.getCreateTime()[0]);
        fpc.createTime().lt(param.getCreateTime() == null || param.getCreateTime().length < 2 ? null : DatetimeTool.nextDay(param.getCreateTime()[1]));
// $bind.field.createTime$

// #bind.field.updateTime#
        fpc.updateTime().ge(param.getUpdateTime() == null || param.getUpdateTime().length == 0 ? null : param.getUpdateTime()[0]);
        fpc.updateTime().lt(param.getUpdateTime() == null || param.getUpdateTime().length < 2 ? null : DatetimeTool.nextDay(param.getUpdateTime()[1]));
// $bind.field.updateTime$

// #bind.field.dictValue#
        fpc.dictValue().eq(param.getDictValue());
// $bind.field.dictValue$

// #bind.field.dictLabel#
        fpc.dictLabel().eq(param.getDictLabel());
// $bind.field.dictLabel$

    }

    public void afterSelectPage(DictDataSelectPage.Param param, Page<DictDataSelectPage.Result> result) {
        log.debug("afterSelectPage-param: {}", JSONUtil.toJsonStr(param));
        log.debug("afterSelectPage-result: {}", JSONUtil.toJsonStr(result));

    }

// entity select auto

// entity search auto
}
